package com.xx.xx;

import java.lang.reflect.Method;
import java.util.Map;

public class FormulasVok {

	public Object vokF(Map<String,Object> map,String methodname){
		Object o = null;
		try {
			StringBuilder a = new StringBuilder("");
			Class clazz = Formulas.class;
			String ss[] = null;
			Method method = clazz.getMethod(methodname, new Class[]{Map.class,String[].class});
			if(method.isAnnotationPresent(GsAnn.class)){
				char[] cFormulas = method.getAnnotation(GsAnn.class).formula().toCharArray();
				int cFormulasLen = cFormulas.length;
				if(cFormulasLen == 0){return null;}
				for(int i=0;i<cFormulasLen;i++){
					if((cFormulas[i]+"").equals("+")||(cFormulas[i]+"").equals("-")
							||(cFormulas[i]+"").equals("*")||(cFormulas[i]+"").equals("/")){
						a.append(","+cFormulas[i]+",");
						
					}else{
						a.append(cFormulas[i]);
					}
				}
			}
			ss = a.toString().split(",");
			o = method.invoke(clazz.newInstance(), map,ss);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return o;
	}
	
	public Object vokF1(Map<String,Object> map,String methodname){
		Object o = null;
		try {
			String type = "0";
			StringBuilder a = new StringBuilder("");
			Class clazz = Formulas.class;
			String ss[] = null;
			Method method = clazz.getMethod(methodname, new Class[]{Map.class,String[].class,String.class});
			if(method.isAnnotationPresent(GsAnn.class)){
				char[] cFormulas = method.getAnnotation(GsAnn.class).formula().toCharArray();
				int cFormulasLen = cFormulas.length;
				if(cFormulasLen == 0){return null;}
				for(int i=0;i<cFormulasLen;i++){
					if((cFormulas[i]+"").equals("+")||(cFormulas[i]+"").equals("-")
							||(cFormulas[i]+"").equals("*")||(cFormulas[i]+"").equals("/")){
						a.append(","+cFormulas[i]+",");
					}else if((cFormulas[i]+"").equals("(")){
						a.append(cFormulas[i]+",");
						type = "1";
					}else if((cFormulas[i]+"").equals(")")){
						a.append(","+cFormulas[i]);
						type = "1";
					}
					else{
						a.append(cFormulas[i]);
					}
				}
			}
			ss = a.toString().split(",");
			o = method.invoke(clazz.newInstance(), map,ss,type);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return o;
	}
	
}
